package com.chinasofti.info_manager.controller;
import com.chinasofti.info_manager.domain.Student;
import com.chinasofti.info_manager.service.StudentService;

import java.util.Scanner;

public abstract class BaseStudentController{
    StudentService Service = new StudentService();

    Scanner sc = new Scanner(System.in);
    //开启学生管理系统
    public final void start() {
        studentLoop:
        while (true){
            System.out.println("---------欢迎来到学生管理系统--------------");
            System.out.println("请您输入：1.添加学生2.删除学生3.修改学生4.查看学生5.退出");
            String choice = sc.next();

            switch (choice){
                case  "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case  "2":
                    //System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case  "3":
//                    System.out.println("修改学生");
                    updateStudent();
                    break;
                case  "4":
                    //System.out.println("查询学生");
                    findAllStudent();
                    break;
                case  "5":
                    System.out.println("感谢使用学生管理系统，再见!");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
    }

    public final void updateStudent() {
        String updateId = inputStudentId();
        Student newStu = inputStudentInfo(updateId);
        Service.updateStudent(updateId,newStu);


    }

    public final void deleteStudentById() {
        String delId = inputStudentId();
        Service.deleteStudentById(delId);
        System.out.println("删除成功");
    }

    private String inputStudentId() {
        String id;
        while(true){
            System.out.println("请输入学生id");
            id = sc.next();
            boolean exists = Service.isExists(id);
            if(exists){
                System.out.println("您输入的id不存在，请重新输入！");
            }else {
                break;
            }
        }
        return id;
    }

    public final void findAllStudent() {
        Student[] stus = Service.findAllStudent();
        //判断stus是否为空？
        if(stus == null){
            System.out.println("查无信息，请添加后重试!");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i=0;i<stus.length;i++){
            Student stu = stus[i];
            if(stu != null){
                System.out.println(stu.getSid()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getBirthday());
            }
        }
    }

    public final void addStudent() {
        String id;
        while(true){
            System.out.println("请输入学生的id：");
            id = sc.next();
            boolean flag = Service.isExists(id);
            if(flag){
                System.out.println("学号可以使用！");
                break;
            }else{
                System.out.println("学号被占用请重新输入!");
            }
        }

        //封装学生对象
        Student stu =  inputStudentInfo(id);
        //开始添加学生到数组
        boolean result = Service.addStudent(stu);
        if(result){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }


    }
    public abstract Student inputStudentInfo(String id);
}
